package com.sjy.ylxt.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sjy.ylxt.entity.basic.ResultBody;
import com.sjy.ylxt.entity.domain.YlxtAppointments;
import com.sjy.ylxt.entity.dto.AddAppointmentDTO;
import com.sjy.ylxt.entity.dto.FeedbackAppointmentDTO;
import com.sjy.ylxt.entity.dto.QueryAppointmentsDTO;
import com.sjy.ylxt.enums.AppointmentStatus;
import com.sjy.ylxt.service.YlxtAppointmentDetailsService;
import com.sjy.ylxt.service.YlxtAppointmentsService;
import com.sjy.ylxt.service.YlxtEscortsService;
import com.sjy.ylxt.service.YlxtPatientService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lils
 * @CreateTime: 2024-12-12
 * @Description: 预约控制层
 */

/**
 * 预约管理
 */
@RestController
@RequestMapping("/appointments")
public class AppointmentsController {


    @Resource
    private YlxtAppointmentsService appointmentsService;

    @Resource
    private YlxtAppointmentDetailsService ylxtAppointmentDetailsService;

    /**
     * 分页查询预约列表
     *
     * @param pageNum       当前页码
     * @param pageSize      每页显示条数
     * @param patientId     患者ID
     * @param companionId   陪诊员ID
     * @param appointmentDate 预约日期
     * @param status        预约状态
     * @param role          用户角色
     * @return              查询结果
     */
    @PostMapping("/page-list")
    public ResultBody<?> pageList(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String patientId,
            @RequestParam(required = false) String companionId,
            @RequestParam(required = false) String appointmentDate,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String role) {
        String userInfo = StpUtil.getLoginDevice();
        // 使用正则表达式或其他方法准确地转换键值对
        Map<String, String> userMap = Arrays.stream(userInfo.replace("{", "").replace("}", "").split(", "))
                .map(entry -> entry.split("="))
                .collect(Collectors.toMap(entry -> entry[0].trim(), entry -> entry[1].trim()));
        QueryAppointmentsDTO dto = new QueryAppointmentsDTO();
        dto.setPageNum(pageNum);
        dto.setPageSize(pageSize);
        dto.setPatientId(patientId);
        dto.setCompanionId(companionId);
        dto.setAppointmentDate(appointmentDate);
        dto.setStatus(status);
        switch (Integer.valueOf(userMap.get("role"))) {
            case 0:
                // 管理员可以查看所有预约
                break;
            case 1:
                // 陪诊员只能查看自己的预约
                dto.setCompanionId(userMap.get("id"));
                break;
            case 2:
                // 患者只能查看自己的预约
                dto.setPatientId(userMap.get("id"));
                break;
            default:
                throw new IllegalArgumentException("未知的用户角色");
        }

        return appointmentsService.pageList(dto);
    }
    /**
     * 预约功能
     * @param patientId
     * @param companionId
     * @param appointmentDate
     * @param serviceType
     * @param location
     * @param description
     * @return
     */
    @PostMapping("/add")
    public ResultBody<?> addAppointments(
            @RequestParam @NotBlank(message = "患者ID不能为空") String patientId,
            @RequestParam @NotBlank(message = "陪诊员ID不能为空") String companionId,
            @RequestParam  String appointmentDate,
            @RequestParam  Integer serviceType,
            @RequestParam @NotBlank(message = "预约地点不能为空") String location,
            @RequestParam @NotBlank(message = "预约描述不能为空") String description) {
        AddAppointmentDTO dto = new AddAppointmentDTO();
        dto.setPatientId(patientId);
        dto.setCompanionId(companionId);
        dto.setAppointmentDate(new Date());
        dto.setServiceType(serviceType);
        dto.setLocation(location);
        dto.setDescription(description);
        return appointmentsService.addAppointments(dto);
    }


    /**
     * 投诉本次预约
     */
    @PostMapping("/complaints")
    public ResultBody<?> complaints(@RequestParam("id") String id,
                                    @RequestParam("complaintContent") String complaintContent){
        return appointmentsService.complaints(id,complaintContent);
    }

    /**
     * 取消预约
     */
    @PostMapping("/cancel")
    public ResultBody<?> cancelAppointments(@RequestParam("id") String id){
        return appointmentsService.cancelAppointments(id);
    }

    @PostMapping("/feedback")
    public ResultBody<?> feedback(
            @RequestParam String id,
            @RequestParam Integer evaluationScore,
            @RequestParam String evaluationContent) {
        FeedbackAppointmentDTO dto = new FeedbackAppointmentDTO();
        dto.setId(id);
        dto.setEvaluationScore(evaluationScore);
        dto.setEvaluationContent(evaluationContent);
        return appointmentsService.feedback(dto);
    }

    /**
     * 查看预约详情
     */
    @GetMapping("/detail")
    public ResultBody<?> detail(@RequestParam("id") String id){
        return ResultBody.success(ylxtAppointmentDetailsService.detail(id));
    }

    @Resource
    private YlxtEscortsService ylxtEscortsService;
    /**
     * 获取陪诊员列表
     */
    @GetMapping("/escorts-list")
    public ResultBody<?> escortsList(){
        return ResultBody.success(ylxtEscortsService.list());
    }
    @Resource
    private YlxtPatientService ylxtPatientService;
    /**
     * 获取患者列表
     */
    @GetMapping("/patient-list")
    public ResultBody<?> patientList(){
        return ResultBody.success(ylxtPatientService.list());
    }

    /**
     * 审核预约
     */
    @PostMapping("/audit")
    public ResultBody<?> audit(@RequestParam String id,
                               @RequestParam Integer status){
        YlxtAppointments one = appointmentsService.getOne(new LambdaQueryWrapper<YlxtAppointments>().eq(
                YlxtAppointments::getId, id
        ));
        if (status == 0){
            //同意预约
            one.setStatus(AppointmentStatus.RESERVED.getCode());
            appointmentsService.updateById(one);
        }else {
            one.setStatus(AppointmentStatus.REJECTED.getCode());
            appointmentsService.updateById(one);
        }
        return ResultBody.success();
    }

}
